{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Necessary Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import csv\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import Orange\n",
    "from Orange.data import Domain, DiscreteVariable, ContinuousVariable\n",
    "from orangecontrib.associate.fpgrowth import *\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Construct and Load the Groceries Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "grocery_items = set()\n",
    "with open(\"grocery_dataset.txt\") as f:\n",
    "    reader = csv.reader(f, delimiter=\",\")\n",
    "    for i, line in enumerate(reader):\n",
    "        grocery_items.update(line)\n",
    "output_list = list()\n",
    "with open(\"grocery_dataset.txt\") as f:\n",
    "    reader = csv.reader(f, delimiter=\",\")\n",
    "    for i, line in enumerate(reader):\n",
    "        row_val = {item:0 for item in grocery_items}\n",
    "        row_val.update({item:1 for item in line})\n",
    "        output_list.append(row_val)\n",
    "grocery_df = pd.DataFrame(output_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Instant food products</th>\n",
       "      <th>UHT-milk</th>\n",
       "      <th>abrasive cleaner</th>\n",
       "      <th>artif. sweetener</th>\n",
       "      <th>baby cosmetics</th>\n",
       "      <th>baby food</th>\n",
       "      <th>bags</th>\n",
       "      <th>baking powder</th>\n",
       "      <th>bathroom cleaner</th>\n",
       "      <th>beef</th>\n",
       "      <th>...</th>\n",
       "      <th>turkey</th>\n",
       "      <th>vinegar</th>\n",
       "      <th>waffles</th>\n",
       "      <th>whipped/sour cream</th>\n",
       "      <th>whisky</th>\n",
       "      <th>white bread</th>\n",
       "      <th>white wine</th>\n",
       "      <th>whole milk</th>\n",
       "      <th>yogurt</th>\n",
       "      <th>zwieback</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 169 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Instant food products  UHT-milk  abrasive cleaner  artif. sweetener  \\\n",
       "0                      0         0                 0                 0   \n",
       "1                      0         0                 0                 0   \n",
       "2                      0         0                 0                 0   \n",
       "3                      0         0                 0                 0   \n",
       "4                      0         0                 0                 0   \n",
       "\n",
       "   baby cosmetics  baby food  bags  baking powder  bathroom cleaner  beef  \\\n",
       "0               0          0     0              0                 0     0   \n",
       "1               0          0     0              0                 0     0   \n",
       "2               0          0     0              0                 0     0   \n",
       "3               0          0     0              0                 0     0   \n",
       "4               0          0     0              0                 0     0   \n",
       "\n",
       "     ...     turkey  vinegar  waffles  whipped/sour cream  whisky  \\\n",
       "0    ...          0        0        0                   0       0   \n",
       "1    ...          0        0        0                   0       0   \n",
       "2    ...          0        0        0                   0       0   \n",
       "3    ...          0        0        0                   0       0   \n",
       "4    ...          0        0        0                   0       0   \n",
       "\n",
       "   white bread  white wine  whole milk  yogurt  zwieback  \n",
       "0            0           0           0       0         0  \n",
       "1            0           0           0       1         0  \n",
       "2            0           0           1       0         0  \n",
       "3            0           0           0       1         0  \n",
       "4            0           0           1       0         0  \n",
       "\n",
       "[5 rows x 169 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grocery_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# View top sold items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "43367\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>item_name</th>\n",
       "      <th>item_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>whole milk</td>\n",
       "      <td>2513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>other vegetables</td>\n",
       "      <td>1903</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>rolls/buns</td>\n",
       "      <td>1809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>soda</td>\n",
       "      <td>1715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>yogurt</td>\n",
       "      <td>1372</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          item_name  item_count\n",
       "0        whole milk        2513\n",
       "1  other vegetables        1903\n",
       "2        rolls/buns        1809\n",
       "3              soda        1715\n",
       "4            yogurt        1372"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_item_count = sum(grocery_df.sum())\n",
    "print(total_item_count)\n",
    "item_summary_df = grocery_df.sum().sort_values(ascending = False).reset_index().head(n=20)\n",
    "item_summary_df.rename(columns={item_summary_df.columns[0]:'item_name',item_summary_df.columns[1]:'item_count'}, inplace=True)\n",
    "item_summary_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Visualize top sold items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.text.Text at 0x2a560a33748>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY8AAAFkCAYAAADYGbemAAAABHNCSVQICAgIfAhkiAAAAAl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Sg5sED6PIAgmBbbfkf+u1IV4o+nTpEsGHInYeQRBMKmQtHmrcduXj/ZaJjKhPIIgmFTk\nnjsN5iH15phW4uyWtDiphPuKVCw3tnfr8zLHPWG2CoJgUmH7ndXHkpYD/q9Q/BzgCuAihudxTTpi\n5xEEwaQmNyKbYXvNgtfebHudUVjWuCd2HkEQTCokHUWKtoJUZWMdoDS57zxJ29k+fyCLm0DEziMI\ngkmFpF0rD58H7rd9VQeZmQyF984PPAM8x1DPmY4tbGc3YucRBMGkIfctf5vtD9WRsx0huk1EYcQg\nCCYNuXvgCpLm7kZe0o8l7S7pVX1e2oQjzFZBEEwqcivZVwPnktodA2VZ4pK2JDV42wxYBbgJuNz2\nEYNZ7fgllEcQBJMKSQe0Grd9UKH8nMD6wJbAnsC/bU+6nUj4PIIgmBRI+rHtjwBPdLtTkHQxyWF+\nDSnfY33bj/VxmROG8HkEQTBZWFfS0sBukhaRtGj1VniMW4FngbWA1wFrSZp3UAsez4TZKgiCSYGk\nvYFPASsDD/3/9u4QB4EYCMPoX4XiNFwIS4Lcu3AbDJo7cJNBNDgEQ9Zs9j3fZtyXtEmbxqu6X/Y6\nJjknWTL/Pj+sOOomiAewK2OMW1Vd/lx7zbwsPyV5ZR5dParqvt6E2yAeAD8aYyyZwXhW1S7/Lv8Q\nDwDaXJgD0CYeALSJBwBt4gFA2xvlkg7zl/yPqQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2a554d642b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "objects = (list(item_summary_df['item_name'].head(n=20)))\n",
    "y_pos = np.arange(len(objects))\n",
    "performance = list(item_summary_df['item_count'].head(n=20))\n",
    " \n",
    "plt.bar(y_pos, performance, align='center', alpha=0.5)\n",
    "plt.xticks(y_pos, objects, rotation='vertical')\n",
    "plt.ylabel('Item count')\n",
    "plt.title('Item sales distribution')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Analyze items contributing to top sales"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>item_name</th>\n",
       "      <th>item_count</th>\n",
       "      <th>item_perc</th>\n",
       "      <th>total_perc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>whole milk</td>\n",
       "      <td>2513</td>\n",
       "      <td>0.057947</td>\n",
       "      <td>0.057947</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>other vegetables</td>\n",
       "      <td>1903</td>\n",
       "      <td>0.043881</td>\n",
       "      <td>0.101829</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>rolls/buns</td>\n",
       "      <td>1809</td>\n",
       "      <td>0.041714</td>\n",
       "      <td>0.143542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>soda</td>\n",
       "      <td>1715</td>\n",
       "      <td>0.039546</td>\n",
       "      <td>0.183089</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>yogurt</td>\n",
       "      <td>1372</td>\n",
       "      <td>0.031637</td>\n",
       "      <td>0.214725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>bottled water</td>\n",
       "      <td>1087</td>\n",
       "      <td>0.025065</td>\n",
       "      <td>0.239791</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>root vegetables</td>\n",
       "      <td>1072</td>\n",
       "      <td>0.024719</td>\n",
       "      <td>0.264510</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>tropical fruit</td>\n",
       "      <td>1032</td>\n",
       "      <td>0.023797</td>\n",
       "      <td>0.288307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>shopping bags</td>\n",
       "      <td>969</td>\n",
       "      <td>0.022344</td>\n",
       "      <td>0.310651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>sausage</td>\n",
       "      <td>924</td>\n",
       "      <td>0.021307</td>\n",
       "      <td>0.331957</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          item_name  item_count  item_perc  total_perc\n",
       "0        whole milk        2513   0.057947    0.057947\n",
       "1  other vegetables        1903   0.043881    0.101829\n",
       "2        rolls/buns        1809   0.041714    0.143542\n",
       "3              soda        1715   0.039546    0.183089\n",
       "4            yogurt        1372   0.031637    0.214725\n",
       "5     bottled water        1087   0.025065    0.239791\n",
       "6   root vegetables        1072   0.024719    0.264510\n",
       "7    tropical fruit        1032   0.023797    0.288307\n",
       "8     shopping bags         969   0.022344    0.310651\n",
       "9           sausage         924   0.021307    0.331957"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "item_summary_df['item_perc'] = item_summary_df['item_count']/total_item_count\n",
    "item_summary_df['total_perc'] = item_summary_df.item_perc.cumsum()\n",
    "item_summary_df.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Analyze items contributing to top 50% of sales"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(19, 4)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "item_summary_df[item_summary_df.total_perc <= 0.5].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>item_name</th>\n",
       "      <th>item_count</th>\n",
       "      <th>item_perc</th>\n",
       "      <th>total_perc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>whole milk</td>\n",
       "      <td>2513</td>\n",
       "      <td>0.057947</td>\n",
       "      <td>0.057947</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>other vegetables</td>\n",
       "      <td>1903</td>\n",
       "      <td>0.043881</td>\n",
       "      <td>0.101829</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>rolls/buns</td>\n",
       "      <td>1809</td>\n",
       "      <td>0.041714</td>\n",
       "      <td>0.143542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>soda</td>\n",
       "      <td>1715</td>\n",
       "      <td>0.039546</td>\n",
       "      <td>0.183089</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>yogurt</td>\n",
       "      <td>1372</td>\n",
       "      <td>0.031637</td>\n",
       "      <td>0.214725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>bottled water</td>\n",
       "      <td>1087</td>\n",
       "      <td>0.025065</td>\n",
       "      <td>0.239791</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>root vegetables</td>\n",
       "      <td>1072</td>\n",
       "      <td>0.024719</td>\n",
       "      <td>0.264510</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>tropical fruit</td>\n",
       "      <td>1032</td>\n",
       "      <td>0.023797</td>\n",
       "      <td>0.288307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>shopping bags</td>\n",
       "      <td>969</td>\n",
       "      <td>0.022344</td>\n",
       "      <td>0.310651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>sausage</td>\n",
       "      <td>924</td>\n",
       "      <td>0.021307</td>\n",
       "      <td>0.331957</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>pastry</td>\n",
       "      <td>875</td>\n",
       "      <td>0.020177</td>\n",
       "      <td>0.352134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>citrus fruit</td>\n",
       "      <td>814</td>\n",
       "      <td>0.018770</td>\n",
       "      <td>0.370904</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>bottled beer</td>\n",
       "      <td>792</td>\n",
       "      <td>0.018263</td>\n",
       "      <td>0.389167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>newspapers</td>\n",
       "      <td>785</td>\n",
       "      <td>0.018101</td>\n",
       "      <td>0.407268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>canned beer</td>\n",
       "      <td>764</td>\n",
       "      <td>0.017617</td>\n",
       "      <td>0.424885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>pip fruit</td>\n",
       "      <td>744</td>\n",
       "      <td>0.017156</td>\n",
       "      <td>0.442041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>fruit/vegetable juice</td>\n",
       "      <td>711</td>\n",
       "      <td>0.016395</td>\n",
       "      <td>0.458436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>whipped/sour cream</td>\n",
       "      <td>705</td>\n",
       "      <td>0.016257</td>\n",
       "      <td>0.474693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>brown bread</td>\n",
       "      <td>638</td>\n",
       "      <td>0.014712</td>\n",
       "      <td>0.489404</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                item_name  item_count  item_perc  total_perc\n",
       "0              whole milk        2513   0.057947    0.057947\n",
       "1        other vegetables        1903   0.043881    0.101829\n",
       "2              rolls/buns        1809   0.041714    0.143542\n",
       "3                    soda        1715   0.039546    0.183089\n",
       "4                  yogurt        1372   0.031637    0.214725\n",
       "5           bottled water        1087   0.025065    0.239791\n",
       "6         root vegetables        1072   0.024719    0.264510\n",
       "7          tropical fruit        1032   0.023797    0.288307\n",
       "8           shopping bags         969   0.022344    0.310651\n",
       "9                 sausage         924   0.021307    0.331957\n",
       "10                 pastry         875   0.020177    0.352134\n",
       "11           citrus fruit         814   0.018770    0.370904\n",
       "12           bottled beer         792   0.018263    0.389167\n",
       "13             newspapers         785   0.018101    0.407268\n",
       "14            canned beer         764   0.017617    0.424885\n",
       "15              pip fruit         744   0.017156    0.442041\n",
       "16  fruit/vegetable juice         711   0.016395    0.458436\n",
       "17     whipped/sour cream         705   0.016257    0.474693\n",
       "18            brown bread         638   0.014712    0.489404"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "item_summary_df[item_summary_df.total_perc <= 0.5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Construct Orange Table "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_assoc_rules = grocery_df\n",
    "domain_grocery = Domain([DiscreteVariable.make(name=item,values=['0', '1']) for item in input_assoc_rules.columns])\n",
    "data_gro_1 = Orange.data.Table.from_numpy(domain=domain_grocery,  X=input_assoc_rules.as_matrix(),Y= None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Prune Dataset for frequently purchased items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def prune_dataset(input_df, length_trans = 2, total_sales_perc = 0.5, start_item = None, end_item = None):\n",
    "    if 'total_items' in input_df.columns:\n",
    "        del(input_df['total_items'])\n",
    "    item_count = input_df.sum().sort_values(ascending = False).reset_index()\n",
    "    total_items = sum(input_df.sum().sort_values(ascending = False))\n",
    "    item_count.rename(columns={item_count.columns[0]:'item_name',item_count.columns[1]:'item_count'}, inplace=True)\n",
    "    if not start_item and not end_item: \n",
    "        item_count['item_perc'] = item_count['item_count']/total_items\n",
    "        item_count['total_perc'] = item_count.item_perc.cumsum()\n",
    "        selected_items = list(item_count[item_count.total_perc < total_sales_perc].item_name)\n",
    "        input_df['total_items'] = input_df[selected_items].sum(axis = 1)\n",
    "        input_df = input_df[input_df.total_items >= length_trans]\n",
    "        del(input_df['total_items'])\n",
    "        return input_df[selected_items], item_count[item_count.total_perc < total_sales_perc]\n",
    "    elif end_item > start_item:\n",
    "        selected_items = list(item_count[start_item:end_item].item_name)\n",
    "        input_df['total_items'] = input_df[selected_items].sum(axis = 1)\n",
    "        input_df = input_df[input_df.total_items >= length_trans]\n",
    "        del(input_df['total_items'])\n",
    "        return input_df[selected_items],item_count[start_item:end_item]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4585, 13)\n",
      "['whole milk', 'other vegetables', 'rolls/buns', 'soda', 'yogurt', 'bottled water', 'root vegetables', 'tropical fruit', 'shopping bags', 'sausage', 'pastry', 'citrus fruit', 'bottled beer']\n"
     ]
    }
   ],
   "source": [
    "output_df, item_counts = prune_dataset(input_df=grocery_df, length_trans=2,total_sales_perc=0.4)\n",
    "print(output_df.shape)\n",
    "print(list(output_df.columns))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Association Rule Mining with FP Growth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_assoc_rules = output_df\n",
    "domain_grocery = Domain([DiscreteVariable.make(name=item,values=['0', '1']) for item in input_assoc_rules.columns])\n",
    "data_gro_1 = Orange.data.Table.from_numpy(domain=domain_grocery,  X=input_assoc_rules.as_matrix(),Y= None)\n",
    "data_gro_1_en, mapping = OneHot.encode(data_gro_1, include_class=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num of required transactions =  45\n"
     ]
    }
   ],
   "source": [
    "min_support = 0.01\n",
    "print(\"num of required transactions = \", int(input_assoc_rules.shape[0]*min_support))\n",
    "num_trans = input_assoc_rules.shape[0]*min_support\n",
    "itemsets = dict(frequent_itemsets(data_gro_1_en, min_support=min_support))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "166886"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(itemsets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Raw rules data frame of 16628 rules generated\n"
     ]
    }
   ],
   "source": [
    "confidence = 0.3\n",
    "rules_df = pd.DataFrame()\n",
    "\n",
    "if len(itemsets) < 1000000: \n",
    "    rules = [(P, Q, supp, conf)\n",
    "    for P, Q, supp, conf in association_rules(itemsets, confidence)\n",
    "       if len(Q) == 1 ]\n",
    "\n",
    "    names = {item: '{}={}'.format(var.name, val)\n",
    "        for item, var, val in OneHot.decode(mapping, data_gro_1, mapping)}\n",
    "    \n",
    "    eligible_ante = [v for k,v in names.items() if v.endswith(\"1\")]\n",
    "    \n",
    "    N = input_assoc_rules.shape[0]\n",
    "    \n",
    "    rule_stats = list(rules_stats(rules, itemsets, N))\n",
    "    \n",
    "    rule_list_df = []\n",
    "    for ex_rule_frm_rule_stat in rule_stats:\n",
    "        ante = ex_rule_frm_rule_stat[0]            \n",
    "        cons = ex_rule_frm_rule_stat[1]\n",
    "        named_cons = names[next(iter(cons))]\n",
    "        if named_cons in eligible_ante:\n",
    "            rule_lhs = [names[i][:-2] for i in ante if names[i] in eligible_ante]\n",
    "            ante_rule = ', '.join(rule_lhs)\n",
    "            if ante_rule and len(rule_lhs)>1 :\n",
    "                rule_dict = {'support' : ex_rule_frm_rule_stat[2],\n",
    "                             'confidence' : ex_rule_frm_rule_stat[3],\n",
    "                             'coverage' : ex_rule_frm_rule_stat[4],\n",
    "                             'strength' : ex_rule_frm_rule_stat[5],\n",
    "                             'lift' : ex_rule_frm_rule_stat[6],\n",
    "                             'leverage' : ex_rule_frm_rule_stat[7],\n",
    "                             'antecedent': ante_rule,\n",
    "                             'consequent':named_cons[:-2] }\n",
    "                rule_list_df.append(rule_dict)\n",
    "    rules_df = pd.DataFrame(rule_list_df)\n",
    "    print(\"Raw rules data frame of {} rules generated\".format(rules_df.shape[0]))\n",
    "    if not rules_df.empty:\n",
    "        pruned_rules_df = rules_df.groupby(['antecedent','consequent']).max().reset_index()\n",
    "    else:\n",
    "        print(\"Unable to generate any rule\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sorting rules in our Grocery Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>consequent</th>\n",
       "      <th>antecedent</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>root vegetables</td>\n",
       "      <td>yogurt, whole milk, tropical fruit</td>\n",
       "      <td>228</td>\n",
       "      <td>0.463636</td>\n",
       "      <td>2.230611</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>sausage</td>\n",
       "      <td>shopping bags, rolls/buns</td>\n",
       "      <td>59</td>\n",
       "      <td>0.393162</td>\n",
       "      <td>2.201037</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>tropical fruit</td>\n",
       "      <td>yogurt, root vegetables, whole milk</td>\n",
       "      <td>92</td>\n",
       "      <td>0.429907</td>\n",
       "      <td>2.156588</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>citrus fruit</td>\n",
       "      <td>whole milk, other vegetables, tropical fruit</td>\n",
       "      <td>66</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>2.125637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>yogurt</td>\n",
       "      <td>whole milk, tropical fruit</td>\n",
       "      <td>199</td>\n",
       "      <td>0.484211</td>\n",
       "      <td>1.891061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>other vegetables</td>\n",
       "      <td>yogurt, whole milk, tropical fruit</td>\n",
       "      <td>228</td>\n",
       "      <td>0.643836</td>\n",
       "      <td>1.826724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>shopping bags</td>\n",
       "      <td>sausage, soda</td>\n",
       "      <td>50</td>\n",
       "      <td>0.304878</td>\n",
       "      <td>1.782992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>bottled water</td>\n",
       "      <td>yogurt, soda</td>\n",
       "      <td>59</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>1.707635</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>whole milk</td>\n",
       "      <td>yogurt, tropical fruit</td>\n",
       "      <td>228</td>\n",
       "      <td>0.754098</td>\n",
       "      <td>1.703222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>rolls/buns</td>\n",
       "      <td>yogurt, whole milk, tropical fruit</td>\n",
       "      <td>97</td>\n",
       "      <td>0.522222</td>\n",
       "      <td>1.679095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>soda</td>\n",
       "      <td>yogurt, sausage</td>\n",
       "      <td>95</td>\n",
       "      <td>0.390625</td>\n",
       "      <td>1.398139</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          consequent                                    antecedent  support  \\\n",
       "4    root vegetables            yogurt, whole milk, tropical fruit      228   \n",
       "5            sausage                     shopping bags, rolls/buns       59   \n",
       "8     tropical fruit           yogurt, root vegetables, whole milk       92   \n",
       "1       citrus fruit  whole milk, other vegetables, tropical fruit       66   \n",
       "10            yogurt                    whole milk, tropical fruit      199   \n",
       "2   other vegetables            yogurt, whole milk, tropical fruit      228   \n",
       "6      shopping bags                                 sausage, soda       50   \n",
       "0      bottled water                                  yogurt, soda       59   \n",
       "9         whole milk                        yogurt, tropical fruit      228   \n",
       "3         rolls/buns            yogurt, whole milk, tropical fruit       97   \n",
       "7               soda                               yogurt, sausage       95   \n",
       "\n",
       "    confidence      lift  \n",
       "4     0.463636  2.230611  \n",
       "5     0.393162  2.201037  \n",
       "8     0.429907  2.156588  \n",
       "1     0.333333  2.125637  \n",
       "10    0.484211  1.891061  \n",
       "2     0.643836  1.826724  \n",
       "6     0.304878  1.782992  \n",
       "0     0.333333  1.707635  \n",
       "9     0.754098  1.703222  \n",
       "3     0.522222  1.679095  \n",
       "7     0.390625  1.398139  "
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(pruned_rules_df[['antecedent','consequent',\n",
    "                  'support','confidence','lift']].groupby('consequent')\n",
    "                                                 .max()\n",
    "                                                 .reset_index()\n",
    "                                                 .sort_values(['lift', 'support','confidence'],\n",
    "                                                              ascending=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Association rule mining on our Online Retail dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load and Filter Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cs_mba = pd.read_excel(io=r'Online Retail.xlsx')\n",
    "cs_mba_uk = cs_mba[cs_mba.Country == 'United Kingdom']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>InvoiceNo</th>\n",
       "      <th>StockCode</th>\n",
       "      <th>Description</th>\n",
       "      <th>Quantity</th>\n",
       "      <th>InvoiceDate</th>\n",
       "      <th>UnitPrice</th>\n",
       "      <th>CustomerID</th>\n",
       "      <th>Country</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>536365</td>\n",
       "      <td>85123A</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>2.55</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>536365</td>\n",
       "      <td>71053</td>\n",
       "      <td>WHITE METAL LANTERN</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>536365</td>\n",
       "      <td>84406B</td>\n",
       "      <td>CREAM CUPID HEARTS COAT HANGER</td>\n",
       "      <td>8</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>2.75</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029G</td>\n",
       "      <td>KNITTED UNION FLAG HOT WATER BOTTLE</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029E</td>\n",
       "      <td>RED WOOLLY HOTTIE WHITE HEART.</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  InvoiceNo StockCode                          Description  Quantity  \\\n",
       "0    536365    85123A   WHITE HANGING HEART T-LIGHT HOLDER         6   \n",
       "1    536365     71053                  WHITE METAL LANTERN         6   \n",
       "2    536365    84406B       CREAM CUPID HEARTS COAT HANGER         8   \n",
       "3    536365    84029G  KNITTED UNION FLAG HOT WATER BOTTLE         6   \n",
       "4    536365    84029E       RED WOOLLY HOTTIE WHITE HEART.         6   \n",
       "\n",
       "          InvoiceDate  UnitPrice  CustomerID         Country  \n",
       "0 2010-12-01 08:26:00       2.55     17850.0  United Kingdom  \n",
       "1 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  \n",
       "2 2010-12-01 08:26:00       2.75     17850.0  United Kingdom  \n",
       "3 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  \n",
       "4 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cs_mba_uk.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remove returned item as we are only interested in the buying patterns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "cs_mba_uk = cs_mba_uk[~(cs_mba_uk.InvoiceNo.str.contains(\"C\") == True)]\n",
    "cs_mba_uk = cs_mba_uk[~cs_mba_uk.Quantity<0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(486286, 8)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cs_mba_uk.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18786,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cs_mba_uk.InvoiceNo.value_counts().shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Build Transaction Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "items = list(cs_mba_uk.Description.unique())\n",
    "grouped = cs_mba_uk.groupby('InvoiceNo')\n",
    "transaction_level_df_uk = grouped.aggregate(lambda x: tuple(x)).reset_index()[['InvoiceNo','Description']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "transaction_dict = {item:0 for item in items}\n",
    "output_dict = dict()\n",
    "temp = dict()\n",
    "for rec in transaction_level_df_uk.to_dict('records'):\n",
    "    invoice_num = rec['InvoiceNo']\n",
    "    items_list = rec['Description']\n",
    "    transaction_dict = {item:0 for item in items}\n",
    "    transaction_dict.update({item:1 for item in items if item in items_list})\n",
    "    temp.update({invoice_num:transaction_dict})\n",
    "\n",
    "new = [v for k,v in temp.items()]\n",
    "tranasction_df = pd.DataFrame(new)\n",
    "del(tranasction_df[tranasction_df.columns[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18786, 4058)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tranasction_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>METAL SIGN HIS DINNER IS SERVED</th>\n",
       "      <th>JAM JAR WITH GREEN LID</th>\n",
       "      <th>SANDWICH BATH SPONGE</th>\n",
       "      <th>KIDS RAIN MAC BLUE</th>\n",
       "      <th>EMPIRE TISSUE BOX</th>\n",
       "      <th>S/2 BEACH HUT TREASURE CHESTS</th>\n",
       "      <th>EMBROIDERED RIBBON REEL DAISY</th>\n",
       "      <th>GOLD/AMBER DROP EARRINGS W LEAF</th>\n",
       "      <th>ANTIQUE GLASS PEDESTAL BOWL</th>\n",
       "      <th>CUPCAKE LACE PAPER SET 6</th>\n",
       "      <th>...</th>\n",
       "      <th>RETROSPOT CANDLE  MEDIUM</th>\n",
       "      <th>LARGE WHITE/PINK ROSE ART FLOWER</th>\n",
       "      <th>DOORMAT UNION JACK GUNS AND ROSES</th>\n",
       "      <th>GLASS BEAD HOOP NECKLACE BLACK</th>\n",
       "      <th>MEDIUM PARLOUR FRAME</th>\n",
       "      <th>RED 3 PIECE RETROSPOT CUTLERY SET</th>\n",
       "      <th>FRENCH WC SIGN BLUE METAL</th>\n",
       "      <th>DOORMAT MERRY CHRISTMAS RED</th>\n",
       "      <th>SMALL POPCORN HOLDER</th>\n",
       "      <th>PINK MURANO TWIST NECKLACE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 4058 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   METAL SIGN HIS DINNER IS SERVED  JAM JAR WITH GREEN LID  \\\n",
       "0                                0                       0   \n",
       "1                                0                       0   \n",
       "2                                0                       1   \n",
       "3                                0                       0   \n",
       "4                                0                       0   \n",
       "\n",
       "   SANDWICH BATH SPONGE  KIDS RAIN MAC BLUE  EMPIRE TISSUE BOX  \\\n",
       "0                     0                   0                  0   \n",
       "1                     0                   0                  0   \n",
       "2                     0                   0                  0   \n",
       "3                     0                   0                  0   \n",
       "4                     0                   0                  0   \n",
       "\n",
       "   S/2 BEACH HUT TREASURE CHESTS  EMBROIDERED RIBBON REEL DAISY   \\\n",
       "0                              0                               0   \n",
       "1                              0                               0   \n",
       "2                              0                               0   \n",
       "3                              0                               0   \n",
       "4                              0                               0   \n",
       "\n",
       "   GOLD/AMBER DROP EARRINGS W LEAF  ANTIQUE GLASS PEDESTAL BOWL  \\\n",
       "0                                0                            0   \n",
       "1                                0                            0   \n",
       "2                                0                            0   \n",
       "3                                0                            0   \n",
       "4                                0                            0   \n",
       "\n",
       "   CUPCAKE LACE PAPER SET 6             ...              \\\n",
       "0                         0             ...               \n",
       "1                         0             ...               \n",
       "2                         0             ...               \n",
       "3                         0             ...               \n",
       "4                         0             ...               \n",
       "\n",
       "   RETROSPOT CANDLE  MEDIUM  LARGE WHITE/PINK ROSE ART FLOWER  \\\n",
       "0                         0                                 0   \n",
       "1                         0                                 0   \n",
       "2                         0                                 0   \n",
       "3                         0                                 0   \n",
       "4                         0                                 0   \n",
       "\n",
       "   DOORMAT UNION JACK GUNS AND ROSES  GLASS BEAD HOOP NECKLACE BLACK  \\\n",
       "0                                  0                               0   \n",
       "1                                  0                               0   \n",
       "2                                  0                               0   \n",
       "3                                  0                               0   \n",
       "4                                  0                               0   \n",
       "\n",
       "   MEDIUM PARLOUR FRAME   RED 3 PIECE RETROSPOT CUTLERY SET  \\\n",
       "0                      0                                  0   \n",
       "1                      0                                  0   \n",
       "2                      0                                  0   \n",
       "3                      0                                  0   \n",
       "4                      0                                  0   \n",
       "\n",
       "   FRENCH WC SIGN BLUE METAL  DOORMAT MERRY CHRISTMAS RED   \\\n",
       "0                          0                             0   \n",
       "1                          0                             0   \n",
       "2                          0                             0   \n",
       "3                          0                             0   \n",
       "4                          0                             0   \n",
       "\n",
       "   SMALL POPCORN HOLDER  PINK MURANO TWIST NECKLACE  \n",
       "0                     0                           0  \n",
       "1                     0                           0  \n",
       "2                     0                           0  \n",
       "3                     0                           0  \n",
       "4                     0                           0  \n",
       "\n",
       "[5 rows x 4058 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tranasction_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4961, 15)\n"
     ]
    }
   ],
   "source": [
    "output_df_uk_n, item_counts_n = prune_dataset(input_df=tranasction_df, length_trans=2, start_item=0, end_item=15)\n",
    "print(output_df_uk_n.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>WHITE HANGING HEART T-LIGHT HOLDER</th>\n",
       "      <th>JUMBO BAG RED RETROSPOT</th>\n",
       "      <th>REGENCY CAKESTAND 3 TIER</th>\n",
       "      <th>PARTY BUNTING</th>\n",
       "      <th>LUNCH BAG RED RETROSPOT</th>\n",
       "      <th>ASSORTED COLOUR BIRD ORNAMENT</th>\n",
       "      <th>SET OF 3 CAKE TINS PANTRY DESIGN</th>\n",
       "      <th>NATURAL SLATE HEART CHALKBOARD</th>\n",
       "      <th>LUNCH BAG  BLACK SKULL.</th>\n",
       "      <th>HEART OF WICKER SMALL</th>\n",
       "      <th>JUMBO BAG PINK POLKADOT</th>\n",
       "      <th>JUMBO SHOPPER VINTAGE RED PAISLEY</th>\n",
       "      <th>JUMBO STORAGE BAG SUKI</th>\n",
       "      <th>PACK OF 72 RETROSPOT CAKE CASES</th>\n",
       "      <th>PAPER CHAIN KIT 50'S CHRISTMAS</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    WHITE HANGING HEART T-LIGHT HOLDER  JUMBO BAG RED RETROSPOT  \\\n",
       "3                                    1                        0   \n",
       "5                                    0                        0   \n",
       "8                                    0                        0   \n",
       "16                                   0                        1   \n",
       "18                                   0                        0   \n",
       "\n",
       "    REGENCY CAKESTAND 3 TIER  PARTY BUNTING  LUNCH BAG RED RETROSPOT  \\\n",
       "3                          1              0                        1   \n",
       "5                          0              0                        0   \n",
       "8                          0              1                        0   \n",
       "16                         1              1                        0   \n",
       "18                         1              1                        0   \n",
       "\n",
       "    ASSORTED COLOUR BIRD ORNAMENT  SET OF 3 CAKE TINS PANTRY DESIGN   \\\n",
       "3                               0                                  1   \n",
       "5                               1                                  0   \n",
       "8                               1                                  1   \n",
       "16                              0                                  0   \n",
       "18                              0                                  0   \n",
       "\n",
       "    NATURAL SLATE HEART CHALKBOARD   LUNCH BAG  BLACK SKULL.  \\\n",
       "3                                 0                        0   \n",
       "5                                 0                        0   \n",
       "8                                 0                        0   \n",
       "16                                0                        0   \n",
       "18                                0                        0   \n",
       "\n",
       "    HEART OF WICKER SMALL  JUMBO BAG PINK POLKADOT  \\\n",
       "3                       0                        0   \n",
       "5                       1                        1   \n",
       "8                       0                        0   \n",
       "16                      1                        1   \n",
       "18                      0                        0   \n",
       "\n",
       "    JUMBO SHOPPER VINTAGE RED PAISLEY  JUMBO STORAGE BAG SUKI  \\\n",
       "3                                   0                       0   \n",
       "5                                   0                       0   \n",
       "8                                   0                       0   \n",
       "16                                  1                       1   \n",
       "18                                  0                       0   \n",
       "\n",
       "    PACK OF 72 RETROSPOT CAKE CASES  PAPER CHAIN KIT 50'S CHRISTMAS   \n",
       "3                                 0                                0  \n",
       "5                                 0                                0  \n",
       "8                                 0                                0  \n",
       "16                                1                                0  \n",
       "18                                0                                0  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_df_uk_n.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Association Rule Mining with FP Growth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_assoc_rules = output_df_uk_n\n",
    "domain_transac = Domain([DiscreteVariable.make(name=item,values=['0', '1']) for item in input_assoc_rules.columns])\n",
    "data_tran_uk = Orange.data.Table.from_numpy(domain=domain_transac,  X=input_assoc_rules.as_matrix(),Y= None)\n",
    "data_tran_uk_en, mapping = OneHot.encode(data_tran_uk, include_class=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num of required transactions =  49\n"
     ]
    }
   ],
   "source": [
    "support = 0.01\n",
    "print(\"num of required transactions = \", int(input_assoc_rules.shape[0]*support))\n",
    "num_trans = input_assoc_rules.shape[0]*support\n",
    "itemsets = dict(frequent_itemsets(data_tran_uk_en, support))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "645632"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(itemsets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Raw rules data frame of 117464 rules generated\n"
     ]
    }
   ],
   "source": [
    "confidence = 0.3\n",
    "rules_df = pd.DataFrame()\n",
    "if len(itemsets) < 1000000: \n",
    "    rules = [(P, Q, supp, conf)\n",
    "    for P, Q, supp, conf in association_rules(itemsets, confidence)\n",
    "       if len(Q) == 1 ]\n",
    "\n",
    "    names = {item: '{}={}'.format(var.name, val)\n",
    "        for item, var, val in OneHot.decode(mapping, data_tran_uk, mapping)}\n",
    "    \n",
    "    eligible_ante = [v for k,v in names.items() if v.endswith(\"1\")]\n",
    "    \n",
    "    N = input_assoc_rules.shape[0]\n",
    "    \n",
    "    rule_stats = list(rules_stats(rules, itemsets, N))\n",
    "    \n",
    "    rule_list_df = []\n",
    "    for ex_rule_frm_rule_stat in rule_stats:\n",
    "        ante = ex_rule_frm_rule_stat[0]            \n",
    "        cons = ex_rule_frm_rule_stat[1]\n",
    "        named_cons = names[next(iter(cons))]\n",
    "        if named_cons in eligible_ante:\n",
    "            rule_lhs = [names[i][:-2] for i in ante if names[i] in eligible_ante]\n",
    "            ante_rule = ', '.join(rule_lhs)\n",
    "            if ante_rule and len(rule_lhs)>1 :\n",
    "                rule_dict = {'support' : ex_rule_frm_rule_stat[2],\n",
    "                             'confidence' : ex_rule_frm_rule_stat[3],\n",
    "                             'coverage' : ex_rule_frm_rule_stat[4],\n",
    "                             'strength' : ex_rule_frm_rule_stat[5],\n",
    "                             'lift' : ex_rule_frm_rule_stat[6],\n",
    "                             'leverage' : ex_rule_frm_rule_stat[7],\n",
    "                             'antecedent': ante_rule,\n",
    "                             'consequent':named_cons[:-2] }\n",
    "                rule_list_df.append(rule_dict)\n",
    "    rules_df = pd.DataFrame(rule_list_df)\n",
    "    print(\"Raw rules data frame of {} rules generated\".format(rules_df.shape[0]))\n",
    "    if not rules_df.empty:\n",
    "        pruned_rules_df = rules_df.groupby(['antecedent','consequent']).max().reset_index()\n",
    "    else:\n",
    "        print(\"Unable to generate any rule\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sort and display rules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>consequent</th>\n",
       "      <th>antecedent</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>PACK OF 72 RETROSPOT CAKE CASES</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER, REGENCY CAKESTAND 3 TIER, NATURAL SLATE HEART CHALKBOARD</td>\n",
       "      <td>145</td>\n",
       "      <td>0.971014</td>\n",
       "      <td>5.394404</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>PAPER CHAIN KIT 50'S CHRISTMAS</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER, REGENCY CAKESTAND 3 TIER, NATURAL SLATE HEART CHALKBOARD</td>\n",
       "      <td>94</td>\n",
       "      <td>0.597701</td>\n",
       "      <td>4.341428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>JUMBO SHOPPER VINTAGE RED PAISLEY</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER, PAPER CHAIN KIT 50'S CHRISTMAS</td>\n",
       "      <td>384</td>\n",
       "      <td>0.879310</td>\n",
       "      <td>4.218819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>LUNCH BAG  BLACK SKULL.</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER, PACK OF 72 RETROSPOT CAKE CASES, LUNCH BAG RED RETROSPOT</td>\n",
       "      <td>227</td>\n",
       "      <td>0.852459</td>\n",
       "      <td>4.078157</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>JUMBO STORAGE BAG SUKI</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER, SET OF 3 CAKE TINS PANTRY DESIGN , JUMBO BAG PINK POLKADOT</td>\n",
       "      <td>405</td>\n",
       "      <td>0.852459</td>\n",
       "      <td>4.016191</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          consequent  \\\n",
       "8    PACK OF 72 RETROSPOT CAKE CASES   \n",
       "9    PAPER CHAIN KIT 50'S CHRISTMAS    \n",
       "3  JUMBO SHOPPER VINTAGE RED PAISLEY   \n",
       "5            LUNCH BAG  BLACK SKULL.   \n",
       "4             JUMBO STORAGE BAG SUKI   \n",
       "\n",
       "                                                                                       antecedent  \\\n",
       "8   WHITE HANGING HEART T-LIGHT HOLDER, REGENCY CAKESTAND 3 TIER, NATURAL SLATE HEART CHALKBOARD    \n",
       "9   WHITE HANGING HEART T-LIGHT HOLDER, REGENCY CAKESTAND 3 TIER, NATURAL SLATE HEART CHALKBOARD    \n",
       "3                             WHITE HANGING HEART T-LIGHT HOLDER, PAPER CHAIN KIT 50'S CHRISTMAS    \n",
       "5    WHITE HANGING HEART T-LIGHT HOLDER, PACK OF 72 RETROSPOT CAKE CASES, LUNCH BAG RED RETROSPOT   \n",
       "4  WHITE HANGING HEART T-LIGHT HOLDER, SET OF 3 CAKE TINS PANTRY DESIGN , JUMBO BAG PINK POLKADOT   \n",
       "\n",
       "   support  confidence      lift  \n",
       "8      145    0.971014  5.394404  \n",
       "9       94    0.597701  4.341428  \n",
       "3      384    0.879310  4.218819  \n",
       "5      227    0.852459  4.078157  \n",
       "4      405    0.852459  4.016191  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dw = pd.options.display.max_colwidth\n",
    "pd.options.display.max_colwidth = 100\n",
    "(pruned_rules_df[['antecedent','consequent',\n",
    "                  'support','confidence','lift']].groupby('consequent')\n",
    "                                                 .max()\n",
    "                                                 .reset_index()\n",
    "                                                 .sort_values(['lift', 'support','confidence'],\n",
    "                                                              ascending=False)).head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pd.options.display.max_colwidth = dw"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
